package com.shadow.cloud.security.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shadow.cloud.security.entity.VersionEntity;
import com.shadow.cloud.security.mapper.VersionMapper;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@AllArgsConstructor
@Service
public class VersionService extends ServiceImpl<VersionMapper, VersionEntity> implements IService<VersionEntity> {

    private final VersionMapper versionMapper;

    public List<VersionEntity> pageVersion(final String query) {
        LambdaQueryWrapper<VersionEntity> wrapper = new LambdaQueryWrapper<VersionEntity>()
                .like(StringUtils.isNotEmpty(query), VersionEntity::getVersionNum, query)
                .orderByDesc(VersionEntity::getCreateTime);
        return versionMapper.selectList(wrapper);
    }

    public void changeActivated(final String id) {
        VersionEntity entity = versionMapper.selectById(id);
        if (Objects.nonNull(entity)) {
            boolean isActivated = Objects.nonNull(entity.getActivated()) &&
                    entity.getActivated() == 1;
            entity.setActivated(isActivated ? 0 : 1);
            versionMapper.updateById(entity);
        }
    }
}
